//
//  YDSwitch.m
//  YunDongGym
//
//  Created by lqq on 2018/5/18.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDSwitch.h"
@interface YDSwitch ()
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
@end

@implementation YDSwitch

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self config];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self config];
    }
    return self;
}


- (void)config
{
    self.layer.cornerRadius = 2;
    self.layer.masksToBounds = YES;
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBtn.titleLabel.font = RegularFont(14);
    leftBtn.layer.masksToBounds = YES;
    [leftBtn addTarget:self action:@selector(choiceLeft) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    _leftBtn = leftBtn;
//    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.top.bottom.equalTo(self);
//        make.width.equalTo(self.mas_width).multipliedBy(0.5);
//    }];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBtn.titleLabel.font = RegularFont(14);
    rightBtn.layer.masksToBounds = YES;
    [rightBtn addTarget:self action:@selector(choiceRight) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    _rightBtn = rightBtn;
//    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.top.bottom.equalTo(self);
//        make.width.equalTo(self.mas_width).multipliedBy(0.5);
//    }];
    
    
    self.index = 0;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _leftBtn.frame = CGRectMake(0, 0, self.width / 2, self.height);
    _rightBtn.frame = CGRectMake(self.width / 2, 0, _leftBtn.width, _leftBtn.height);
}

- (void)choiceLeft
{
    self.index = 0;
    if ([self.delegate respondsToSelector:@selector(choiceIndex:)]) {
        [self.delegate choiceIndex:0];
    }
    
}
- (void)choiceRight
{
    self.index = 1;
    if ([self.delegate respondsToSelector:@selector(choiceIndex:)]) {
        [self.delegate choiceIndex:1];
    }
}

- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    [_leftBtn setTitle:SAFE_VALUE_AT_INDEX(_titles, 0) forState:UIControlStateNormal];
    [_rightBtn setTitle:SAFE_VALUE_AT_INDEX(_titles, 1) forState:UIControlStateNormal];
}

- (void)setIndex:(NSInteger)index
{
    _index = index;
    switch (index) {
        case 1:
        {
            _rightBtn.backgroundColor = HEXColor(@"#3AA7FF");
            [_rightBtn setTitleColor:HEXColor(@"#FFFFFF") forState:UIControlStateNormal];
            _rightBtn.layer.cornerRadius = 2;
            _leftBtn.backgroundColor = HEXColor(@"#F4F6F9");
            [_leftBtn setTitleColor:HEXColor(@"#999999") forState:UIControlStateNormal];
            _leftBtn.layer.cornerRadius = 0;

        }
            break;
            
        default:
        {
            _leftBtn.backgroundColor = HEXColor(@"#3AA7FF");
            [_leftBtn setTitleColor:HEXColor(@"#FFFFFF") forState:UIControlStateNormal];
            _leftBtn.layer.cornerRadius = 2;
            _rightBtn.backgroundColor = HEXColor(@"#F4F6F9");
            [_rightBtn setTitleColor:HEXColor(@"#999999") forState:UIControlStateNormal];
            _rightBtn.layer.cornerRadius = 0;
        }
            break;
    }
}



@end
